<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-auth="'RoleManagerPriv.Add'">
          新增消息
        </a-button>
        <a-button
          type="primary"
          @click="handleSetReadFlagBatch"
          v-auth="'RoleManagerPriv.Add'"
          :loading="loadingSetReadFlag"
          :disabled="getRowSelection().selectedRowKeys.length == 0"
        >
          标为已读
        </a-button>
        <a-button
          color="error"
          @click="handleDeleteBatch"
          v-auth="'RoleManagerPriv.Add'"
          :loading="loadingDel"
          :disabled="getRowSelection().selectedRowKeys.length == 0"
        >
          批量删除
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:eye-solid',
              tooltip: '标为已读',
              onClick: handleSetReadFlag.bind(null, record),
            },
            {
              icon: 'clarity:info-standard-line',
              tooltip: '查看',
              onClick: handleMessageDetail.bind(null, record),
            },
            {
              icon: 'clarity:chat-bubble-line',
              tooltip: '回复',
              onClick: handleReplay.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              tooltip: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <MessageModal @register="registerModal" @success="handleSuccess" />
    <MessageDetailModal @register="registerModal2" />
    <MessageReplayModal @register="registerModal3" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getMessageList, setreadflag, delMessage } from '/@/api/system/system';

  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import MessageModal from './MessageModal.vue';
  import MessageDetailModal from './MessageDetailModal.vue';
  import MessageReplayModal from './MessageReplayModal.vue';

  import { columns, searchFormSchema } from './message.data';

  export default defineComponent({
    name: 'MessageManager',
    components: { BasicTable, TableAction, MessageModal, MessageDetailModal, MessageReplayModal },
    setup() {
      const { createMessage, createConfirm } = useMessage();
      const loadingSetReadFlag = ref(false);
      const loadingDel = ref(false);
      const [registerTable, { reload, getSelectRowKeys, clearSelectedRowKeys, getRowSelection }] =
        useTable({
          rowKey: 'id',
          title: '消息列表',
          api: getMessageList,
          columns,
          formConfig: {
            labelWidth: 120,
            schemas: searchFormSchema,
          },
          rowSelection: {
            type: 'checkbox',
          },
          useSearchForm: true,
          showTableSetting: true,
          bordered: true,
          showIndexColumn: false,
          actionColumn: {
            width: 220,
            title: '操作',
            dataIndex: 'action',
            slots: { customRender: 'action' },
            fixed: undefined,
          },
        });

      function handleSuccess() {
        reload();
      }

      /***************************消息Modal***********************************/

      const [registerModal, { openModal }] = useModal();

      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        });
      }

      async function handleSetReadFlag(record: Recordable) {
        loadingSetReadFlag.value = true;
        await setreadflag(record.id);
        clearSelectedRowKeys();
        reload();
        loadingSetReadFlag.value = false;
        createMessage.success('操作成功');
      }

      async function handleSetReadFlagBatch() {
        loadingSetReadFlag.value = true;
        await setreadflag(getSelectRowKeys().join());
        clearSelectedRowKeys();
        reload();
        loadingSetReadFlag.value = false;
        createMessage.success('操作成功');
      }

      async function handleDelete(record: Recordable) {
        await delMessage(record.id, 'toUser');
        reload();
        createMessage.success('删除成功');
      }

      async function handleDeleteBatch() {
        createConfirm({
          iconType: 'warning',
          title: '确定要删除吗？删除后无法恢复！',
          okType: 'danger',
          async onOk() {
            loadingDel.value = true;
            await delMessage(getSelectRowKeys().join(), 'toUser');
            clearSelectedRowKeys();
            reload();
            loadingDel.value = false;
          },
        });
      }

      /***************************消息详情Modal***********************************/
      const [registerModal2, { openModal: openDetailModal }] = useModal();

      function handleMessageDetail(record: Recordable) {
        openDetailModal(true, { record });
      }

      /***************************消息回复Modal***********************************/
      const [registerModal3, { openModal: openReplayModal }] = useModal();

      function handleReplay(record: Recordable) {
        openReplayModal(true, { record });
      }

      return {
        registerTable,
        handleCreate,
        handleSuccess,
        registerModal,
        getSelectRowKeys,
        handleSetReadFlag,
        handleSetReadFlagBatch,
        loadingSetReadFlag,
        handleDelete,
        handleDeleteBatch,
        loadingDel,
        getRowSelection,
        registerModal2,
        handleMessageDetail,
        registerModal3,
        handleReplay,
      };
    },
  });
</script>
